package com.quasar.hpatient.view.adapter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.quasar.hpatient.R;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.custommodel.InspactionBean;
import com.quasar.hpatient.model.custommodel.InspactionitemBean;
import com.quasar.hpatient.utils.LogAndToastUtil;
import com.quasar.hpatient.utils.OutliersValues;
import com.quasar.hpatient.utils.Spanny;
import com.quasar.hpatient.utils.StringUtils;
import com.quasar.hpatient.widget.WidgetNumber;
import com.quasar.hpatient.widget.WidgetProteinNumber;
import com.vise.log.ViseLog;

import java.util.ArrayList;
import java.util.List;


/**
 * name : lishuai
 * time : 2017/10/25
 * title:化验记录
 */

public class TestrecordsItemAdapter extends BaseMultiItemQuickAdapter<InspactionBean, BaseViewHolder> {


    private OnItemEditTextChangedListener mListener;

    public void setListener(OnItemEditTextChangedListener listener) {
        mListener = listener;
    }

    public TestrecordsItemAdapter(List data) {
        super(data);
        addItemType(InspactionBean.TITLE, R.layout.widget_test_title);
        addItemType(InspactionBean.EDIT, R.layout.widget_test_edit);
        addItemType(InspactionBean.FROM, R.layout.widget_test_from);
        addItemType(InspactionBean.DIV_TITLE, R.layout.widget_test_div_title);
        addItemType(InspactionBean.DIV_EDIT, R.layout.widget_test_div_edit);
        addItemType(InspactionBean.SUBMIT, R.layout.widget_test_div_title);
        //蛋白质
        addItemType(InspactionBean.PPOTEIN, R.layout.widget_protein_test_from);

    }

    //显示隐藏布局
    public void setHideView(BaseViewHolder helper, final InspactionBean item) {


        RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) helper.itemView.getLayoutParams();
        if (item.getInspactionitemBeen().getIsHide() != null && item.getInspactionitemBeen().getIsHide().length() > 0) {
            helper.itemView.setVisibility(View.GONE);
            param.height = 0;
            param.width = 0;
        } else {
            param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            param.width = LinearLayout.LayoutParams.MATCH_PARENT;
            helper.itemView.setVisibility(View.VISIBLE);
        }
    }

    public void nofocusable() {
        TextView et_name = helper.getView(R.id.name);
        if (et_name != null) {
            et_name.setFocusable(false);
        }

    }

    @Override
    protected void convert(final BaseViewHolder helper, final InspactionBean item) {

        this.helper = helper;

        //保存添加的值
        switch (helper.getItemViewType()) {
            case InspactionBean.TITLE:
                if (item.getInspactionName() != null) {
                    helper.setText(R.id.formname, item.getInspactionName());  //填充化验单名称
                }
                break;
            case InspactionBean.FROM:
                setHideView(helper, item);
                final EditText value1 = helper.getView(R.id.value1);
                EditText unitdoseName=helper.getView(R.id.unit);
                if (item.getInspactionitemBeen() != null) {

                    if (item.getInspactionitemBeen().getItemName() != null) {
                        helper.setText(R.id.name, item.getInspactionitemBeen().getItemName()); //填充化验单小项名称
                    }
                    //加载检测值
                    if (item.getInspactionitemBeen().getItemValue() != null) {
                        helper.setText(R.id.value1, item.getInspactionitemBeen().getItemValue());  //填充化验单小项值

                        if (OutliersValues.editTextNum(item.getInspactionitemBeen().getItemValue())) {
                            int tc = OutliersValues.TextColor(
                                    item.getInspactionitemBeen().getItemValue(),
                                    item.getInspactionitemBeen().getStandardValue());
                            textCore(tc, value1,0);

                        }
                    }

                    //保存输入的检测项值
                    value1.setFilters(new InputFilter[]{new InputFilter.LengthFilter(7)});
                    value1.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                            value1.setSelection(s.length());
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {

                            String str = s.toString();


                            int selectionStart = value1.getSelectionStart();
                            int selectionEnd = value1.getSelectionEnd();
                            if (s.toString() != null && s.toString().length() >3) {
                                if (!StringUtils.isOnlyPointNumber(s.toString(), 3)) {
                                    s.delete(selectionStart - 1, selectionEnd);
                                    value1.setText(s);
                                    value1.setSelection(s.length());
                                }
                            }

                            if(s.toString().equals("00")){
                                value1.setText("");
                                LogAndToastUtil.toast(mContext, "请输入正确数值");
                            }
                            if(s.toString().equals("0.000")){
                                value1.setText("");
                                LogAndToastUtil.toast(mContext, "请输入正确数值");
                            }


                            if (s.toString().trim().substring(0).equals(".")) {
                                value1.setText("0" + s);
                                value1.setSelection(2);
                            }
                            if (value1.getText().toString().indexOf(".") >= 0) {
                                if (value1.getText().toString().indexOf(".", value1.getText().toString().indexOf(".") + 1) > 0) {
//                                    tv_numOfChar.setText("已经输入\".\"不能重复输入");
                                    value1.setText(value1.getText().toString().substring(0, value1.getText().toString().length() - 1));
                                    value1.setSelection(value1.getText().toString().length());
                                }

                            }

                            if (item.getInspactionitemBeen().getDoseName().equals("%")) {

                                int posDot = str.indexOf(".");
                                //小数点之前保留3位数字或者一千
                                if (posDot <= 0) {
                                    //temp
                                    if (str.equals("100")) {
                                        return;
                                    } else {
                                        if (str.length() <= 2) {
                                            return;
                                        } else {
                                            s.delete(2, 3);
                                            return;
                                        }
                                    }
                                }
                            }


                            mListener.OnEditeValue(s, helper.getLayoutPosition());
                            InspactionBean inspactionBean = TestrecordsItemAdapter.this.getData().get(helper.getLayoutPosition() - 1);

                            if (OutliersValues.editTextNum(str)) {

                                // TODO: 2018/3/23  
                                int tc = OutliersValues.TestingTextColor(
                                        s.toString(),
                                        item.getInspactionitemBeen().getStandardValue(),
                                        inspactionBean.getInspactionitemBeen().getStandardValue(),0);//乘以10 或者0.1

                                ViseLog.d("查询边界值+++++"+item.getInspactionitemBeen().getBoundaryVal());
                                if(item.getInspactionitemBeen().getBoundaryVal()!=null && item.getInspactionitemBeen().getBoundaryVal().length()>0
                                       ){
//                                    && !item.getInspactionitemBeen().getBoundaryVal().equals("0-0")

                                    textCore(tc, value1,1);
                                    int boundaryVal = OutliersValues.TestingTextColor(
                                            s.toString(),
                                            item.getInspactionitemBeen().getBoundaryVal(),
                                            inspactionBean.getInspactionitemBeen().getBoundaryVal(),1);//不用乘以10 或者0.1

                                    if(boundaryVal==OutliersValues.BOUNDARYVALUE){
                                        value1.setText("");
                                    }
                                }else{
                                    textCore(tc, value1,0);
                                    if (tc == OutliersValues.BOUNDARYVALUE) {
                                        value1.setText("");
                                    }
                                }
                            }
                        }
                    });


                    if (item.getInspactionitemBeen().getStandardValue() != null) {  //填充化验单小项参考值
                        helper.setText(R.id.msgCheck, item.getInspactionitemBeen().getStandardValue());
                    }
                    mListener.OnMsgCheck(item.getInspactionitemBeen().getStandardValue(), helper.getLayoutPosition());
                    if (item.getInspactionitemBeen().getDoseName() != null) {  //填充化验单小项单位
                        unitdoseName.setText(item.getInspactionitemBeen().getDoseName());
                    }
                    //检测是否为默认值
                    Spanny spanny1 = new Spanny();
                    if (item.getInspactionitemBeen().getAttribute() == ConstantManger.DISPLAYVIEW) {
                        spanny1.append("移除", new ForegroundColorSpan(color(R.color.textcolor)));
                        helper.setText(R.id.edit_delete, spanny1);
                    } else if(item.getInspactionitemBeen().getAttribute() == ConstantManger.CUSTOMVIEW) {
                        spanny1.append("移除", new ForegroundColorSpan(color(R.color.values_err)));
                        helper.setText(R.id.edit_delete, spanny1);
                    }else{
                        spanny1.append("移除", new ForegroundColorSpan(color(R.color.values_err)));
                        helper.setText(R.id.edit_delete, spanny1);
                    }

                }

                //单位修改
                unitdoseName.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        mListener.OnUnit(s, helper.getLayoutPosition());
                    }
                });


                //参考值修改
                final TextView msgCheck = helper.getView(R.id.msgCheck);
                msgCheck.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                        builder.setTitle("参考值");
                        View views = LayoutInflater.from(mContext).inflate(R.layout.dialog_edit_test_min_max, null);
                        builder.setView(views);
                        final EditText edit_min = (EditText) views.findViewById(R.id.edit_min);
                        final EditText edit_max = (EditText) views.findViewById(R.id.edit_max);

                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //确定后操作
                                try {
                                    float min_val = Float.parseFloat(edit_min.getText().toString());
                                    float max_val = Float.parseFloat(edit_max.getText().toString());
                                    if (min_val >= 0 && max_val > 0) {
                                        if (min_val > max_val) {
                                            Toast.makeText(mContext, "最小值不能大于最大值", Toast.LENGTH_SHORT).show();
                                        } else {
                                            msgCheck.setText(min_val + "-" + max_val);

                                            mListener.OnMsgCheck(min_val + "-" + max_val, helper.getLayoutPosition());
                                        }
                                    }
                                } catch (Exception e) {
                                    Toast.makeText(mContext, "参考值格式填写错误", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });

                        builder.show();
                    }
                });

                break;
            case InspactionBean.EDIT:
                if (item.getAdd() == true) {
                    helper.setText(R.id.additem, "增加");
                }
                break;

            //新增的item布局
            case InspactionBean.DIV_EDIT:

                setHideView(helper, item);

                Spanny spanny1 = new Spanny();
                spanny1.append("移除", new ForegroundColorSpan(color(R.color.values_err)));
                helper.setText(R.id.edit_delete1, spanny1);
                //名称
                final EditText values = helper.getView(R.id.name);

                //检测名称
                if (item.getInspactionitemBeen().getItemName() != null) {
                    helper.setText(R.id.name, item.getInspactionitemBeen().getItemName());  //填充化验单小项值
                } else {
                    helper.setText(R.id.name, "");  //填充化验单小项值
                }


                values.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (s != null) {
                            mListener.OnItemName(s, helper.getLayoutPosition());
                            ViseLog.d("新增name" + s.toString());
                            itemname=s.toString();
                        }
                    }
                });

                final List<String> strings = new ArrayList<>();
                values.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                                                    @Override
                                                    public void onFocusChange(View v, boolean hasFocus) {

                                                        if (hasFocus) {
                                                            for (int i = 0; i < TestrecordsItemAdapter.this.getData().size(); i++) {
                                                                if(TestrecordsItemAdapter.this.getData().get(i).getInspactionitemBeen()!=null){
                                                                    if (TestrecordsItemAdapter.this.getData().get(i).getInspactionitemBeen().getItemName()!=null) {
                                                                        strings.add(TestrecordsItemAdapter.this.getData().get(i).getInspactionitemBeen().getItemName());
                                                                    }
                                                                }
                                                            }
                                                        } else {
                                                            for (int aa = 0; aa < strings.size(); aa++) {
                                                                if (itemname.equals(strings.get(aa))) {
                                                                    values.setFocusable(true);
                                                                    mListener.LoseFocus(helper.getLayoutPosition());
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                );


                //检测值value
                final EditText value2 = helper.getView(R.id.value);
                value2.setFilters(new InputFilter[]{new InputFilter.LengthFilter(7)});
                if (item.getInspactionitemBeen().getItemValue() != null) {
                    helper.setText(R.id.value, item.getInspactionitemBeen().getItemValue());  //填充化验单小项值
                    String str = item.getInspactionitemBeen().getItemValue().toString();
                    if (OutliersValues.editTextNum(str)) {
                        int tc = OutliersValues.TextColor(
                                str,
                                item.getInspactionitemBeen().getStandardValue());
                        textCore(tc, value2,0);

                    }

                } else {
                    helper.setText(R.id.value, "");  //填充化验单小项值
                }

                value2.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        value2.setSelection(s.length());
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                        String str = s.toString();

                        int selectionStart = value2.getSelectionStart();
                        int selectionEnd = value2.getSelectionEnd();
                        if (s.toString() != null && s.toString().length() >3) {
                            if (!StringUtils.isOnlyPointNumber(s.toString(), 3)) {
                                s.delete(selectionStart - 1, selectionEnd);
                                value2.setText(s);
                                value2.setSelection(s.length());
                            }
                        }

                        if(s.toString().equals("00")){
                            value2.setText("");
                            LogAndToastUtil.toast(mContext, "请输入正确数值");
                        }
                        if(s.toString().equals("0.000")){
                            value2.setText("");
                            LogAndToastUtil.toast(mContext, "请输入正确数值");
                        }

                        if (s.toString().trim().substring(0).equals(".")) {
                            value2.setText("0" + s);
                            value2.setSelection(2);
                        }
                        if (value2.getText().toString().indexOf(".") >= 0) {
                            if (value2.getText().toString().indexOf(".", value2.getText().toString().indexOf(".") + 1) > 0) {
//                                    tv_numOfChar.setText("已经输入\".\"不能重复输入");
                                value2.setText(value2.getText().toString().substring(0, value2.getText().toString().length() - 1));
                                value2.setSelection(value2.getText().toString().length());
                            }

                        }

                        if (item.getInspactionitemBeen().getDoseName().equals("%")) {

                            int posDot = str.indexOf(".");
                            //小数点之前保留3位数字或者一千
                            if (posDot <= 0) {
                                //temp
                                if (str.equals("100")) {
                                    return;
                                } else {
                                    if (str.length() <= 2) {
                                        return;
                                    } else {
                                        s.delete(2, 3);
                                        return;
                                    }
                                }
                            }
                        }


                        InspactionBean inspactionBean = TestrecordsItemAdapter.this.getData().get(helper.getLayoutPosition() - 1);
                        if (OutliersValues.editTextNum(str)) {
                            int tc = OutliersValues.TestingTextColor(
                                    str,
                                    item.getInspactionitemBeen().getStandardValue()
                                    , inspactionBean.getInspactionitemBeen().getStandardValue(),0);
                            textCore(tc, value2,0);

                            if (tc == OutliersValues.BOUNDARYVALUE) {
                                value2.setText("");
                            }

                        }

                        mListener.OnEditeValue(s, helper.getLayoutPosition());
                    }
                });

                //参考值修改
                final TextView CheckValues = helper.getView(R.id.msgCheck);
                if (item.getInspactionitemBeen().getStandardValue() != null) {
                    helper.setText(R.id.msgCheck, item.getInspactionitemBeen().getStandardValue());  //填充化验单小项值
                } else {
                    helper.setText(R.id.msgCheck, "");  //填充化验单小项值
                }
                CheckValues.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        ViseLog.d("msgCheck");

                        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                        builder.setTitle("参考值");
                        View views = LayoutInflater.from(mContext).inflate(R.layout.dialog_edit_test_min_max, null);
                        builder.setView(views);
                        final EditText edit_min = (EditText) views.findViewById(R.id.edit_min);
                        final EditText edit_max = (EditText) views.findViewById(R.id.edit_max);

                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //确定后操作
                                try {
                                    float min_val = Float.parseFloat(edit_min.getText().toString());
                                    float max_val = Float.parseFloat(edit_max.getText().toString());
                                    if (min_val >= 0 && max_val > 0) {
                                        if (min_val > max_val) {
                                            Toast.makeText(mContext, "最小值不能大于最大值", Toast.LENGTH_SHORT).show();
                                        } else {
                                            CheckValues.setText(min_val + "-" + max_val);

                                            mListener.OnMsgCheck(min_val + "-" + max_val, helper.getLayoutPosition());
                                        }
                                    }
                                } catch (Exception e) {
                                    Toast.makeText(mContext, "参考值格式填写错误", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });

                        builder.show();
                    }
                });


                //单位unit
                EditText value4 = helper.getView(R.id.unit);

                if (item.getInspactionitemBeen().getDoseName() != null) {
                    helper.setText(R.id.unit, item.getInspactionitemBeen().getDoseName());  //填充化验单小项值
                } else {
                    helper.setText(R.id.unit, "");  //填充化验单小项值
                }

                value4.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        mListener.OnUnit(s, helper.getLayoutPosition());
                    }
                });

                //是否为默认项edit_delete1

                TextView value5 = helper.getView(R.id.edit_delete1);
                value5.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        //此处判断输入的检测值是否正常
                        mListener.OnAttribute(ConstantManger.CUSTOMVIEW, helper.getLayoutPosition());
                    }
                });

                break;


            case InspactionBean.SUBMIT:
                //保存所有化验大项名称
                final List<String> TestName = new ArrayList<>();

                final EditText inspectionname = helper.getView(R.id.inspectionname);
                inspectionname.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        mListener.OnInspectionName(s, "", helper.getLayoutPosition());
                        testNames = s.toString();
                    }
                });


                //失去焦点，判断化验大项名称是否存在
                inspectionname.setOnFocusChangeListener(
                        new View.OnFocusChangeListener() {
                            @Override
                            public void onFocusChange(View v, boolean hasFocus) {
                                if (hasFocus) {
                                    //获取焦点
                                    for (int i = 0; i < TestrecordsItemAdapter.this.getData().size(); i++) {
                                        if (TestrecordsItemAdapter.this.getData().get(i).getItemType() == 1) {
                                            TestName.add(TestrecordsItemAdapter.this.getData().get(i).getInspactionName());
                                        }
                                    }
                                    badname = testNames;

                                } else {
                                    //失去焦点
                                    for (int a = 0; a < TestName.size(); a++) {
                                        if (testNames.equals(TestName.get(a))) {
                                            inspectionname.setText("");

                                            mListener.OnInspectionName(null, "", helper.getLayoutPosition());
                                            LogAndToastUtil.toast(mContext, "不能添加重复项目");
                                        }
                                    }
                                }
                            }
                        }
                );

                break;


            //蛋白质项目
            case InspactionBean.PPOTEIN:
                setHideView(helper, item);
                TextView testName = helper.getView(R.id.name);//检测名称
                WidgetProteinNumber testVal = helper.getView(R.id.wn_protein);//检测值
                final TextView testReference = helper.getView(R.id.msgCheck);//参考值
                final TextView testUnit = helper.getView(R.id.unit);//单位
                TextView testDelete = helper.getView(R.id.edit_delete);//操作

                if (item.getInspactionitemBeen().getItemName() != null) {
                    testName.setText(item.getInspactionitemBeen().getItemName());
                }

                //数据为空的或着数据长度为0的时候不进入判断
                if (item.getInspactionitemBeen().getItemValue() != null && item.getInspactionitemBeen().getItemValue().length()>0) {
                    if (OutliersValues.editTextNum(item.getInspactionitemBeen().getItemValue())) {
                        Float val = Float.parseFloat(item.getInspactionitemBeen().getItemValue());
                        testVal.setNumber(val);
                    }else{
                        Float val = Float.parseFloat(item.getInspactionitemBeen().getItemValue());
                        testVal.setNumber(val);
                    }
                }

                if (item.getInspactionitemBeen().getStandardValue() != null) {//参考值
                    testReference.setText(item.getInspactionitemBeen().getStandardValue());
                }

                if (item.getInspactionitemBeen().getDoseName() != null) {  //单位
                    testUnit.setText(item.getInspactionitemBeen().getDoseName());
                }

                //检测是否为默认值
                Spanny spanny = new Spanny();
                if (item.getInspactionitemBeen().getAttribute() == ConstantManger.DISPLAYVIEW) {
                    spanny.append("移除", new ForegroundColorSpan(color(R.color.textcolor)));
                    testDelete.setText(spanny);
                } else {
                    spanny.append("移除", new ForegroundColorSpan(color(R.color.values_err)));
                    testDelete.setText(spanny);
                }

                testReference.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        ViseLog.d("msgCheck");

                        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                        builder.setTitle("参考值");
                        View views = LayoutInflater.from(mContext).inflate(R.layout.dialog_edit_test_min_max, null);
                        builder.setView(views);
                        final EditText edit_min = (EditText) views.findViewById(R.id.edit_min);
                        final EditText edit_max = (EditText) views.findViewById(R.id.edit_max);

                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //确定后操作
                                try {
                                    float min_val = Float.parseFloat(edit_min.getText().toString());
                                    float max_val = Float.parseFloat(edit_max.getText().toString());
                                    if (min_val > 0 && max_val > 0) {
                                        if (min_val > max_val) {
                                            Toast.makeText(mContext, "最小值不能大于最大值", Toast.LENGTH_SHORT).show();
                                        } else {
                                            testReference.setText(min_val + "-" + max_val);

                                            mListener.OnMsgCheck(min_val + "-" + max_val, helper.getLayoutPosition());
                                        }
                                    }
                                } catch (Exception e) {
                                    Toast.makeText(mContext, "参考值格式填写错误", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });

                        builder.show();
                    }
                });


                //检测值发生改变时记录该值
                testVal.setLetfTextFwatch(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        mListener.OnEditeValue(s, helper.getLayoutPosition());
                    }
                });

                break;

        }
        helper.addOnClickListener(R.id.additem);
        helper.addOnClickListener(R.id.name);
        helper.addOnClickListener(R.id.edit_delete);
        helper.addOnClickListener(R.id.closelist);  //点击隐藏或者显示
//        helper.addOnClickListener(R.id.msgCheck); //参考值编辑
        helper.addOnClickListener(R.id.edit_delete1);
        helper.addOnClickListener(R.id.inspection_save);
        helper.addOnClickListener(R.id.all);//全部

    }


    String itemname = "";
    String testNames = "";
    String badname;

    BaseViewHolder helper;

    public void test() {
        TextView inspection_save = helper.getView(R.id.inspection_save);
        if (inspection_save != null) {
            inspection_save.setFocusable(false);
        }
    }


    //创建回调接口保存已经填写的检测值
    public interface OnItemEditTextChangedListener {
        void OnItemName(Editable editable, int position);

        void OnEditeValue(Editable editable, int position);

        void OnMsgCheck(String editable, int position);

        void OnUnit(Editable editable, int position);

        void OnAttribute(int editable, int position);

        void OnInspectionName(Editable editable, String testVal, int position); //大项名称

        void LoseFocus(int position);

    }

    private int color(int value) {
        return mContext.getResources().getColor(value);
    }


    private void textCore(int textColor, TextView tv,int stats) {

        switch (textColor) {
            //正常值
            case OutliersValues.NORMALVALUE:
                tv.setTextColor(color(R.color.btcolor));
                break;

            //大于最大值
            case OutliersValues.UPNORMALVALUE:
                tv.setTextColor(color(R.color.values_err));
                break;

            //小于最小值
            case OutliersValues.LOWNORMALVALUE:
                tv.setTextColor(color(R.color.values_di));
                break;


            case OutliersValues.DANGERLVALUE:
                tv.setTextColor(color(R.color.values_err));
                break;

            case OutliersValues.BOUNDARYVALUE:
                ViseLog.d("BOUNDARYVALUE" + OutliersValues.BOUNDARYVALUE);
                // msg.setError("请核对数据");
                if(stats==0){
                    LogAndToastUtil.toast(mContext, "非法检测值", "");
                }
                break;

        }
    }
}
